package view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import beans.*;
import java.util.*;
import javax.swing.*;


public class PalavrasChaveView extends AbstractGUIView implements IPalavrasChaveView{

	private ActionListener listener = null;
	private Vector<Topico> topico = null;
	private String action = null;
		
	private JList  kList=null;
	private DefaultListModel lista=null;
	private JPanel BackPanel = null;
	private JScrollPane scrollPane=null;
	
//	public TopicosView(JPanel BackPanel) {
//		this.BackPanel = BackPanel;
//		topico=new Vector<Topico>();
//		buildGUI();
//		
//	}
//	
//	public TopicosView(JPanel BackPanel, Vector<Topico> topico) {
//		this.topicos = topico;
//		this.BackPanel = BackPanel;
//		buildGUI();
//	}
	
	public PalavrasChaveView() {
		topico=new Vector<Topico>();
		BackPanel = new JPanel();
		BackPanel.setLayout(new BoxLayout(BackPanel, BoxLayout.Y_AXIS));
		BackPanel.setPreferredSize(new Dimension(120, 0));
		scrollPane = new JScrollPane();
		lista=new DefaultListModel();
		buildGUI();
	}
	
	public JPanel getPanel() {
		return BackPanel;
	}
	
	@Override
	public void show() throws Exception {
		BackPanel.setVisible(true);
	}	public PalavrasChaveView(JPanel BackPanel) {
		this.BackPanel = BackPanel;
		topico=new Vector<Topico>();
		buildGUI();
		
	}
	
	public PalavrasChaveView(JPanel BackPanel, Vector<Topico> topico) {
		this.topico = topico;
		this.BackPanel = BackPanel;
		buildGUI();
	}

	@Override
	public void refresh() {
		
	}

	@Override
	public void hide() {
		BackPanel.setVisible(false);
	}

	@Override
	public void setActionListener(ActionListener listener) {
		this.listener = listener;
	}

	@Override
	public Vector<Topico> getTopicos() {
		return topico;
	}

	@Override
	public void setTopicos(Vector<Topico> topico) {
		this.topico=topico;
	}

	public PalavrasChaveView getView() {
		return this;
	}
	
	@Override
	public void showError(Exception e) {
		JOptionPane.showMessageDialog(BackPanel, e.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
	}
	
	public void addTopico(Topico topico) {
		topico.add(topico);
		addListaTopicos();
	}
	
	//private void buildGUI(JPanel BackPanel) {
	private void buildGUI() {
		//addListaTopicos();
		kList= new JList(lista);
		scrollPane.add(kList);
		BackPanel.add(scrollPane);
		kList.addListSelectionListener(new SelecaoListaListener());
		BackPanel.setVisible(true);
		scrollPane.setVisible(true);
		kList.setVisible(true);
	}
	
	private DefaultListModel addListaTopicos () {
		lista.clear();
		for(int i=0;i<topico.size();i++)
			lista.addElement(topico.get(i).getNome());
		return lista;
	}

	private class SelecaoListaListener implements ListSelectionListener {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			action=topico.get(1).ACT_UPDATE;
			listener.actionPerformed(new ActionEvent(getView(),topico.get(1).ID_TOPICO, action));
		}			
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			
			SwingUtilities.invokeLater(new Runnable() {
				//String [] tp1={"Topico1","Topico2"};
				//String [] tp2={"Topico3","Topico4"};
				//String [] tp3={"Topico5","Topico6"};
				
				
				public void run() {
					PalavrasChaveView o=new PalavrasChaveView();
					Topico topico=new Topico();
					topico.setNome("Topico de teste");
					o.addTopico(topico);
					try {
						JFrame n= new JFrame();
						//n.setContentPane(o.getPanel());
						n.add(o.getPanel());
						n.setSize(816, 478);
						n.setLayout(new FlowLayout());
						n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						n.setVisible(true);
						}
					catch (Exception e) { e.printStackTrace();}
				}
			});
		}
		catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		catch ( Exception e) {
			e.printStackTrace();
		}
	}
}
